package com.lifesea.news.api.service;

import com.github.pagehelper.PageHelper;
import com.lifesea.commons.utils.Utils;
import com.lifesea.dao.mybatis.MedSourceMapper;
import com.lifesea.dao.mybatis.MedTagsMapper;
import com.lifesea.dao.mybatis.extra.MedNewsExtMapper;
import com.lifesea.model.MedNews;
import com.lifesea.model.MedNewsExample;
import com.lifesea.model.MedSource;
import com.lifesea.model.MedTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;

@Service()
public class MedNewsService {

    @Autowired
    private MedNewsExtMapper medNewsExtMapper;

    @Autowired
    private MedTagsMapper medTagsMapper;

    @Autowired
    private MedSourceMapper medSourceMapper;

    private MedNewsExample getExample(HashMap map){
        MedNewsExample example = new MedNewsExample();
        MedNewsExample.Criteria criteria = example.createCriteria();

        if (map != null && map.size() > 0) {
            if (Utils.hasKeyNotEmpty(map, "orderbyclause")) example.setOrderByClause(Utils.toString(map.get("orderbyclause")));
            if (Utils.hasKeyNotEmpty(map, "title")) criteria.andTitleLike("%" + Utils.toString(map.get("title") + "%"));
            if (Utils.hasKeyNotEmpty(map, "category")) criteria.andCategoryEqualTo(Utils.toInteger(map.get("category")));
            if (Utils.hasKeyNotEmpty(map, "source")) criteria.andSourceEqualTo(Utils.toInteger(map.get("source")));
            if (Utils.hasKeyNotEmpty(map, "is_hot")) criteria.andIsHotEqualTo(Utils.toBoolean(map.get("is_hot")));
            if (Utils.hasKeyNotEmpty(map, "is_recommended")) criteria.andIsRecommendedEqualTo(Utils.toBoolean(map.get("is_recommended")));
            if (Utils.hasKeyNotEmpty(map, "news_status")) criteria.andNewsStatusEqualTo(Utils.toBoolean(map.get("news_status")));

        }

        return example;
    }

    public Integer count(HashMap map) {
        return medNewsExtMapper.countByExample(getExample(map));
    }

    public List<MedNews> findAll() {
        return findAll(null);
    }

    public List<MedNews> findAll(HashMap map) {
        return medNewsExtMapper.selectByExample(getExample(map));
    }

    public List<MedNews> findAll(HashMap map, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return medNewsExtMapper.selectByExample(getExample(map));
    }

    public MedNews findOne(Long id) {
        return medNewsExtMapper.selectByPrimaryKey(id);
    }

    public Integer save(MedNews record){
        updateNum(record,1);
        return medNewsExtMapper.insertSelective(record);
    }

    public Integer update(MedNews medNews) {
        if(medNews.getCategory() != null || medNews.getSource() != null){
            MedNews oldNews = findOne(medNews.getId());
            if(medNews.getCategory() != null){
                if(medNews.getCategory().intValue() != oldNews.getCategory().intValue()){
                    updateTagNum(oldNews,-1);
                    updateTagNum(medNews,1);
                }
            }
            if(medNews.getSource() != null){
                if(medNews.getSource().intValue() != oldNews.getSource().intValue()){
                    updateSourceNum(oldNews,-1);
                    updateSourceNum(medNews,1);
                }
            }
        }
        return medNewsExtMapper.updateByPrimaryKeySelective(medNews);
    }

    public Integer delete(Long id){
        MedNews medNews = findOne(id);
        updateNum(medNews,-1);
        return medNewsExtMapper.deleteByPrimaryKey(id);
    }

    private void updateNum(MedNews record,int num){
        updateTagNum(record,num);
        updateSourceNum(record,num);
    }

    private void updateTagNum(MedNews record,int num){

        HashMap args = new HashMap();

        if(record.getCategory()!= null && record.getCategory() > 0){
            //获取当前个数
            args.clear();
            args.put("category",record.getCategory());
            List<MedNews> medNewsList = findAll(args);
            int size = 0;
            if(medNewsList != null){
                size = medNewsList.size();
            }
            size = size+ num;
            if(size < 0){
                size =0;
            }
            MedTags tags = medTagsMapper.selectByPrimaryKey(record.getCategory());
            if(tags != null){
                MedTags updateTags = new MedTags();
                updateTags.setId(tags.getId());
                updateTags.setNum(size);
                medTagsMapper.updateByPrimaryKey(updateTags);
            }
        }
    }

    private void updateSourceNum(MedNews record,int num){

        HashMap args = new HashMap();

        if(record.getSource() != null && record.getSource() > 0){
            //获取当前个数
            args.clear();
            args.put("source",record.getSource());
            List<MedNews> medNewsList = findAll(args);
            int size = 0;
            if(medNewsList != null){
                size = medNewsList.size();
            }
            size = size+ num;
            if(size < 0){
                size =0;
            }
            MedSource source = medSourceMapper.selectByPrimaryKey(record.getSource());
            if(source != null){
                MedSource medSource = new MedSource();
                medSource.setId(source.getId());
                medSource.setNum(size);
                medSourceMapper.updateByPrimaryKey(medSource);
            }
        }
    }

    public int remove_issuance_time(MedNews news){
        return medNewsExtMapper.remove_issuance_time(news);
    }

    public int praise(MedNews news){
        return medNewsExtMapper.praise(news);
    }

}
